package com.guigu.qingcheng.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.guigu.qingcheng.mapper.TbReturnCauseMapper;
import com.guigu.qingcheng.pojo.TbReturnCause;
import com.guigu.qingcheng.service.TbReturnCauseService;
import com.guigu.qingcheng.utils.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;


/**
*
* @author Jiang
* @date 2020/07/31
*/
@Service
public class TbReturnCauseServiceImpl implements TbReturnCauseService {
    @Autowired(required = false)
    private TbReturnCauseMapper tbReturnCauseMapper;



    @Override
    public List<TbReturnCause> queryAll() {
        List<TbReturnCause> tbReturnCause = tbReturnCauseMapper.selectAll();
            return tbReturnCause;
    }

    @Override
    public PageUtil<TbReturnCause> findListAll(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        Page<TbReturnCause> pages = (Page<TbReturnCause>) tbReturnCauseMapper.selectAll();
        PageUtil<TbReturnCause> pageUtil=new PageUtil<>();
        pageUtil.setRows(pages.getResult());
        pageUtil.setTotal(pages.getTotal());
        return pageUtil;
    }


    @Override
    public PageUtil<TbReturnCause> pageQueryByAll(Integer pageNum, Integer pageSize, TbReturnCause tbReturnCause) {

        Example example=new Example(TbReturnCause.class);
        Example.Criteria criteria = example.createCriteria();
        // TODO 待实现 重新实现判断参数

        //if(!StringUtils.isEmpty(brand.getId())){
        //    criteria.andEqualTo("id",brand.getId());
        //}
        //if(!StringUtils.isEmpty(brand.getName())){
        //   criteria.andLike("name","%"+brand.getName()+"%");
        //}
        PageHelper.startPage(pageNum,pageSize);
        Page<TbReturnCause> pages = (Page<TbReturnCause>) tbReturnCauseMapper.selectByExample(example);
        PageUtil<TbReturnCause> pageUtil=new PageUtil<>();
        pageUtil.setRows(pages.getResult());
        pageUtil.setTotal(pages.getTotal());
        return pageUtil;
     }


    @Override
    public boolean saveTbReturnCause(TbReturnCause tbReturnCause) {
        int insert = tbReturnCauseMapper.insert(tbReturnCause);
        if(insert>0){
            return true;
        }
        return false;
    }

    @Override
    public boolean updateTbReturnCause(TbReturnCause tbReturnCause) {
        int i = tbReturnCauseMapper.updateByPrimaryKeySelective(tbReturnCause);
        if(i>0){
            return true;
        }
        return false;
    }


    @Override
    public boolean deleteById(Integer id) {
        int i = tbReturnCauseMapper.deleteByPrimaryKey(id);
        if(i>0){
            return true;
        }
        return false;
    }

    @Override
    public TbReturnCause findById(Integer id) {
        TbReturnCause tbReturnCause = tbReturnCauseMapper.selectByPrimaryKey(id);
        return tbReturnCause;
    }

    @Override
    public boolean batchDelete(String ids) {
        String[] id = ids.split(",");
        for (int i=0;i<id.length;i++){
            tbReturnCauseMapper.deleteByPrimaryKey(id[i]);
        }
        return true;
    }
}
